<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

use think\Config;
use think\Session;
use fast\Http;

class Jdy extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    protected $key = "";

    const JDY_TOKEN_T = "__jdytoken__";
    const JDY_COOKIE_T = "__jdysection__";
    const JDY_TIMER_T = "__jdytimer__";

    public function _initialize()
    {
        parent::_initialize();

        $this->key = [0xCC, 0x22, 0x4F, 0x58, 0x88, 0x34, 0x28, 0x25, 0x35, 0x79, 0x51, 0xCB, 0x30,
            0x40, 0x36, 0x28, 0x35, 0x29, 0x11, 0x2B, 0x40, 0xA8, 0x76, 0x68];
        $this->key = $this->tostr($this->key);
    }

    //字节数组转字符串
    function tostr($bytes)
    {
        $str = '';
        foreach ($bytes as $ch) {
            $str .= chr($ch);
        }
        return $str;
    }

    /**
     * PHP发送Json对象数据
     *
     * @param $url 请求url
     * @param $jsonStr 发送的json字符串
     * @return array
     */
    function http_post_json($url, $jsonStr)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
//                'Content-Type: text/plain; charset=utf-8',
                'Content-Length: ' . strlen($jsonStr)
            )
        );
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        return array($httpCode, $response);
    }

    //http://acciot.test/api/jdy/encrypt?input=asasd
    //加密
    public function encrypt($input)
    {
        $method = 'DES-EDE3';
        $data = openssl_encrypt($input, $method, $this->key, OPENSSL_RAW_DATA);
        $data = strtoupper(bin2hex($data));
//        echo $data;
        return $data;
//        $this->success('返回成功', $data);
    }

    //登录 yourslink
    //http://acciot.test/api/jdy/loginyourslink
    //http://acciot.megowx.com/api/jdy/loginyourslink
    public function loginyourslink()
    {
        $token = $this->regetToken();
        if (empty($token)) {
            $this->error('返回失败');
        }
        $this->success('返回成功', $token);
    }

    //http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?queryDatas&RequestInfoJson={"action":"Buildings","token":"xxxxxxxx",”page”:1,”rows”:20,”remark”:””}
    //http://acciot.megowx.com/api/jdy/queryDatas?action=Buildings
    //http://acciot.test/api/jdy/queryDatas?action=Rooms
    //Buildings/Rooms/Devices/devHistories
    public function queryDatas()
    {
        $action = $this->request->request("action", "devHistories");
        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?requestToken&username=enaviapi&password=enavi28960818";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);// 获取头部信息
        curl_setopt($ch, CURLOPT_HEADER, 1);// 返回原生的（Raw）输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
        $content = curl_exec($ch);
//        curl_close($ch);
        list($header, $body) = explode("\r\n\r\n", $content);
        preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
        $cookie = $matches[1];
        $data = $body;
//        echo $cookie.'<br><br>';
//        exit();

        $token = "";
        $ret = json_decode($data, TRUE);
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $token = $ret['message'];
        }

        if (empty($token)) {
            echo $data;
            exit();
        }

        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?queryDatas";
        $inputA = array(
            "action" => $action,
            "token" => $token,
            "page" => 1,
            "rows" => 20,
            "remark" => "",
        );
        $input = json_encode($inputA);
//        echo $input;

        $url .= "&requestInfoJson=" . $input;
//        echo '<br>'.$url;
//        $ch = curl_init(); // 如果前面没有curl_close就不需要curl_init，前面curl_close也不影响
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        $data = curl_exec($ch);
//        echo '<br>'.$data;
        curl_close($ch);

        $ret = json_decode($data, TRUE);
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $message = $ret['message'];
            echo '<br>' . $message;
            $data = json_decode($message, TRUE);
            $total = $data["total"];
            $rows = $data["rows"];
            $size = count($rows);
        }

    }

    public function queryDatas1()
    {
        $action = $this->request->request("action", "Buildings");

        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?requestToken&username=enaviapi&password=enavi28960818";
        $data = Http::get($url);

        $token = "";
        $ret = json_decode($data, TRUE);
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $token = $ret['message'];
        }

        if (empty($token)) {
            echo $data;
        }

        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?queryDatas";
        $inputA = array(
            "action" => $action,
            "token" => $token,
            "page" => 1,
            "rows" => 20,
            "remark" => "",
        );
        $input = json_encode($inputA);
        $url .= "&requestInfoJson=" . $input;
        echo $url;

        $data = Http::get($url);
        echo '<br>' . $data;
//        $ret = json_decode($data, TRUE);

    }

    public function post($url, $post_data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }

    //获取失败了，重新获取token
    function regetToken()
    {
        $token = "";

        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?requestToken&username=enaviapi&password=enavi28960818";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);// 获取头部信息
        curl_setopt($ch, CURLOPT_HEADER, 1);// 返回原生的（Raw）输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
        $content = curl_exec($ch);
        curl_close($ch);

        if ($content) {
            list($header, $body) = explode("\r\n\r\n", $content);
            preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
            $cookie = $matches[1];
            $data = $body;


            $ret = json_decode($data, TRUE);
            if ($ret['result'] && $ret['msg_code'] == 0) {
                $token = $ret['message'];
                Session::set(self::JDY_TOKEN_T, $token);
                Session::set(self::JDY_COOKIE_T, $cookie);
            }
        }

        return $token;
    }

    //http://acciot.test/api/jdy/getrooms
    public function getrooms()
    {
        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?requestToken&username=enaviapi&password=enavi28960818";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);// 获取头部信息
        curl_setopt($ch, CURLOPT_HEADER, 1);// 返回原生的（Raw）输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
        $content = curl_exec($ch);
        list($header, $body) = explode("\r\n\r\n", $content);
        preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
        $cookie = $matches[1];
        $data = $body;

        $token = "";
        $ret = json_decode($data, TRUE);
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $token = $ret['message'];
        }

        if (empty($token)) {
            echo $data;
            exit();
        }

        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?queryDatas&requestInfoJson=";

        $inputA = array(
            "action" => "Rooms",
            "token" => $token,
            "page" => 1,
            "rows" => 500,
            "remark" => "",
        );
        $input = json_encode($inputA);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $urlA = $url . $input;
        curl_setopt($ch, CURLOPT_URL, $urlA);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
        $data = curl_exec($ch);
        curl_close($ch);
        $ret = json_decode($data, TRUE);
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $message = $ret['message'];
            echo '<br>' . $message;
            $data = json_decode($message, TRUE);
            $total = $data["total"];
            $rows = $data["rows"];
            $size = count($rows);
            echo '<br>' . $data;
        }
    }

    //http://acciot.test/api/jdy/getdevices?remark=23741-21
    public function getdevices()
    {
//        set_time_limit(0); //执行时间无限
//        ini_set('memory_limit', '-1'); //内存无限
//
//        ob_end_clean();
//        ob_implicit_flush();
//        header('X-Accel-Buffering: no');
        $remark = $this->request->request("remark");
        if (!$remark) {
            return;
        }

        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?requestToken&username=enaviapi&password=enavi28960818";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);// 获取头部信息
        curl_setopt($ch, CURLOPT_HEADER, 1);// 返回原生的（Raw）输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
        $content = curl_exec($ch);
        list($header, $body) = explode("\r\n\r\n", $content);
        preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
        $cookie = $matches[1];
        $data = $body;

        $token = "";
        $ret = json_decode($data, TRUE);
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $token = $ret['message'];
        }

        //获取装置
        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?queryDatas";
        $inputA = array(
            "action" => "Devices",
            "token" => $token,
            "remark" => $remark,
        );

        $input = json_encode($inputA);
        $url .= "&requestInfoJson=" . $input;
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
        $data = curl_exec($ch);
        curl_close($ch);
        $ret = json_decode($data, TRUE);
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $message = $ret['message'];
            echo '<br>' . $message;
            $data = json_decode($message, TRUE);
            $total = $data["total"];
            $rows = $data["rows"];
            $size = count($rows);
        }
    }

    //http://acciot.test/api/jdy/getdevHistories?remark=10525301010339,2019-09-15,2019-09-23
    public function getdevHistories()
    {
        $data0 = date('H:i:s');

        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        ob_end_clean();
        ob_implicit_flush();
        header('X-Accel-Buffering: no');

        $remark = $this->request->request("remark");
        if (!$remark) {
            return;
        }
        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?requestToken&username=enaviapi&password=enavi28960818";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);// 获取头部信息
        curl_setopt($ch, CURLOPT_HEADER, 1);// 返回原生的（Raw）输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
        $content = curl_exec($ch);
        list($header, $body) = explode("\r\n\r\n", $content);
        preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
        $cookie = $matches[1];
        $data = $body;

        $token = "";
        $ret = json_decode($data, TRUE);
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $token = $ret['message'];
        }

        if (empty($token)) {
            echo $data;
            exit();
        }

        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?queryDatas";
        $inputA = array(
            "action" => "devHistories",
            "token" => $token,
            "remark" => $remark,
        );
        $input = json_encode($inputA);
        $url .= "&requestInfoJson=" . $input;
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
        $data = curl_exec($ch);
        curl_close($ch);

        $ret = json_decode($data, TRUE);
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $message = $ret['message'];
//            echo '<br>' . $message;
            $data = json_decode($message, TRUE);
            $total = $data["total"];
            $devHistorieslist = $data["rows"];
            $size = count($devHistorieslist);

            foreach ($devHistorieslist as $row) {
                $device = \app\admin\model\jdy\Devhistory::where('devhistoryid', $row['id'])->find();
                if (!$device) {
                    $device = new \app\admin\model\jdy\Devhistory();
                }
                $device->devhistoryid = $row['id'];
                $device->sn = $row['sn'];
                $device->t = $row['time'];
                $device->d = $row['date'];
                $device->online = $row['online'];
                $device->state = $row['state'];
                $device->dt = $row['durationTime'];
                $device->dateInfo = $row['dateInfo'];
                $device->save();
            }

            echo $total.'<br>';
        }

        echo $data0.' ~ ' . date('H:i:s') . '<br>';
    }

    //http://acciot.test/api/jdy/getalldevHistories
    //取所有的记录
    public function getalldevHistories()
    {
        $data0 = date('H:i:s');

        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        ob_end_clean();
        ob_implicit_flush();
        header('X-Accel-Buffering: no');

        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?requestToken&username=enaviapi&password=enavi28960818";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);// 获取头部信息
        curl_setopt($ch, CURLOPT_HEADER, 1);// 返回原生的（Raw）输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
        $content = curl_exec($ch);
        list($header, $body) = explode("\r\n\r\n", $content);
        preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
        $cookie = $matches[1];
        $data = $body;

        $token = "";
        $ret = json_decode($data, TRUE);
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $token = $ret['message'];
        }

        if (empty($token)) {
            echo $data;
            exit();
        }

        $list = \app\admin\model\jdy\Device::field('deviceid')->select();
        foreach ($list as $row) {
            $deviceid = $row['deviceid'];
//            $deviceid = '10525301010339';
            $device = \app\admin\model\jdy\Devhistory::where('sn', $deviceid)->order('dateInfo desc')->limit(1)->find();

            $from = "2019-01-01";
            $to = date("Y-m-d", time());
            if ($device) {
                $from = date("Y-m-d", $device['dateInfo']/1000);
            }
//            remark=10525301010339,2019-01-01,2019-09-05
            $remark = $row['deviceid'].','.$from.','.$to;

            $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?queryDatas";
            $inputA = array(
                "action" => "devHistories",
                "token" => $token,
                "remark" => $remark,
            );
            $input = json_encode($inputA);
            $url .= "&requestInfoJson=" . $input;
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_COOKIE, $cookie);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
            $data = curl_exec($ch);

            $ret = json_decode($data, TRUE);
            if ($ret['result'] && $ret['msg_code'] == 0) {
                $message = $ret['message'];
//            echo '<br>' . $message;
                $data = json_decode($message, TRUE);
                $total = $data["total"];
                $devHistorieslist = $data["rows"];
                $size = count($devHistorieslist);

                foreach ($devHistorieslist as $row) {
                    $device = \app\admin\model\jdy\Devhistory::where('devhistoryid', $row['id'])->find();
                    if (!$device) {
                        $device = new \app\admin\model\jdy\Devhistory();
                    }
                    $device->devhistoryid = $row['id'];
                    $device->sn = $row['sn'];
                    $device->t = $row['time'];
                    $device->d = $row['date'];
                    $device->online = $row['online'];
                    $device->state = $row['state'];
                    $device->dt = $row['durationTime'];
                    $device->dateInfo = $row['dateInfo'];
                    $device->save();
                }
            }

            echo $deviceid . '<br>';
        }
        curl_close($ch);
        echo $data0.' ~ ' . date('H:i:s') . '<br>';

    }

    //http://acciot.test/api/jdy/getalldata
    //每分钟取一次装置信息，每10分钟取一次房间信息； token提示无效后重新获取token
    public function getalldata0()
    {
        $data0 = date('H:i:s');

        $token = Session::get(self::JDY_TOKEN_T);
        if (!$token) {
            $token = $this->regetToken();
        }

        $linkstatus=  \app\admin\model\jdy\Linkstatus::get(1);
        if (empty($token)) {
            //没有联上服务器
            $linkstatus->status = "断线";
            $linkstatus->save();
            return;
        }
        $linkstatus->status = "正常";
        $linkstatus->save();

        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        ob_end_clean();
        ob_implicit_flush();
        header('X-Accel-Buffering: no');

        $cookie = Session::get(self::JDY_COOKIE_T);
        if (!$cookie) {
            return;
        }

        $timernum = Session::get(self::JDY_TIMER_T);
        if (!$timernum) {
            $timernum = 0;
        } else {
            $timernum = intval($timernum);
        }
        $timernum++;
        Session::set(self::JDY_TIMER_T, $timernum . "");

        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?queryDatas&requestInfoJson=";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);

        $rooms = \app\admin\model\jdy\Room::all();
//        $timernum = 1;
        if (!$rooms || $timernum % 10 == 1) {
            //间隔10分钟取一次Rooms
            $inputA = array(
                "action" => "Rooms",
                "token" => $token,
                "page" => 1,
                "rows" => 500,
                "remark" => "",
            );
            $input = json_encode($inputA);
            curl_setopt($ch, CURLOPT_URL, $url . $input);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
            $data = curl_exec($ch);
            $ret = json_decode($data, TRUE);
            $rooms = [];
            if ($ret['result'] && $ret['msg_code'] == 0) {
                $message = $ret['message'];
                echo '<br>' . $message;
                $data = json_decode($message, TRUE);
                $total = $data["total"];
                $roomlist = $data["rows"];
                $size = count($roomlist);
                //rooms里已有building信息
                foreach ($roomlist as $row) {
                    $building0 = $row['building'];
                    $buildingid = 0;
                    if ($building0) {
                        $building = \app\admin\model\jdy\Building::where('buildingid', $building0['id'])->find();
                        if (!$building) {
                            $building = new \app\admin\model\jdy\Building();
                        }
                        $building->buildingid = $building0['id'];
                        $building->name = $building0['name'];
                        $building->floorNumber = $building0['floorNumber'];
                        $building->address = $building0['address'];
                        $building->optId = $building0['optId'];
                        $building->remark = $building0['remark'];
//                        $building->updatenum = $building->updatenum + 1;
                        $building->save();
                        $buildingid = $building->id;
                    }

                    $room = \app\admin\model\jdy\Room::where('roomid', $row['id'])->find();
                    if (!$room) {
                        $room = new \app\admin\model\jdy\Room();
                    }
                    $room->building_id = $buildingid;
                    $room->roomid = $row['id'];
                    $room->name = $row['name'];
                    $room->account = $row['account'];
                    $room->password = $row['password'];
                    $room->floorNo = $row['floorNo'];
                    $room->optId = $row['optId'];
                    $room->padId = $row['padId'];
                    $room->remark = $row['remark'];
//                    $room->updatenum = $room->updatenum + 1;
                    $room->save();

                    $room = $room->toArray();
                    $rooms[] = $room;
                }
            }
        } else {
            $rooms = collection($rooms)->toArray();
        }

        $num = 0;
        foreach ($rooms as $room) {
            //获取装置
            $inputA = array(
                "action" => "Devices",
                "token" => $token,
                "remark" => $room['padId'],
            );
            $input = json_encode($inputA);
            curl_setopt($ch, CURLOPT_URL, $url . $input);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
            $data = curl_exec($ch);
            $ret = json_decode($data, TRUE);
            if ($ret['result'] && $ret['msg_code'] == 0) {
                $message = $ret['message'];
//                echo '<br>' . $message;
                $data = json_decode($message, TRUE);
                $total = $data["total"];
                $devicelist = $data["rows"];
                $size = count($devicelist);

                foreach ($devicelist as $row) {
                    $device = \app\admin\model\jdy\Device::where('deviceid', $row['id'])->find();
                    if (!$device) {
                        $device = new \app\admin\model\jdy\Device();
                    }
                    $device->room_id = $room['id'];
                    $device->deviceid = $row['id'];
                    $device->category = $row['category'];
                    $device->type = $row['type'];
                    $device->pid = $row['pid'];
                    $device->name = $row['name'];
                    $device->place = $row['place'];
                    $device->version = $row['version'];
                    $device->online = $row['online'];
                    $device->iconVer = $row['iconVer'];
//                    $device->iconUrl = $row['iconUrl'];
                    $device->center = $row['center'];
                    $device->deviceIp = $row['deviceIp'];
                    $device->routeMacString = $row['routeMacString'];
                    $device->subType = $row['subType'];
                    $device->state = $row['state'];
//                    $device->updatenum = $device->updatenum + 1;
                    $device->save();

                    $num++;
                }
            }
        }
        curl_close($ch);

        echo $num.'<br>';
        echo $data0.' ~ ' . date('H:i:s') . '<br>';
    }

    //每天0点获取一次，for 新建案场绑定room，更新room，building，device
    public function getalldata()
    {
        $data0 = date('H:i:s');

        $token = $this->regetToken();
        if (empty($token)) {
            return;
        }

        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        ob_end_clean();
        ob_implicit_flush();
        header('X-Accel-Buffering: no');

        $cookie = Session::get(self::JDY_COOKIE_T);
        if (!$cookie) {
            return;
        }

        $url = "http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?queryDatas&requestInfoJson=";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);

        $inputA = array(
            "action" => "Rooms",
            "token" => $token,
            "page" => 1,
            "rows" => 5000,
            "remark" => "",
        );
        $input = json_encode($inputA);
        curl_setopt($ch, CURLOPT_URL, $url . $input);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
        $data = curl_exec($ch);
        if (empty($data)) {
            return;
        }

        $ret = json_decode($data, TRUE);
        $rooms = [];

        //重新获取rooms
        if ($ret['result'] && $ret['msg_code'] == 0) {
            $message = $ret['message'];
            echo '<br>' . $message;
            $data = json_decode($message, TRUE);
            $total = $data["total"];
            $roomlist = $data["rows"];
            $size = count($roomlist);
            //rooms里已有building信息
            foreach ($roomlist as $row) {
                $building0 = $row['building'];
                $buildingid = 0;
                if ($building0) {
                    $building = \app\admin\model\jdy\Building::where('buildingid', $building0['id'])->find();
                    if (!$building) {
                        $building = new \app\admin\model\jdy\Building();
                    }
                    $building->buildingid = $building0['id'];
                    $building->name = $building0['name'];
                    $building->floorNumber = $building0['floorNumber'];
                    $building->address = $building0['address'];
                    $building->optId = $building0['optId'];
                    $building->remark = $building0['remark'];
//                        $building->updatenum = $building->updatenum + 1;
                    $building->save();
                    $buildingid = $building->id;
                }

                $room = \app\admin\model\jdy\Room::where('roomid', $row['id'])->find();
                if (!$room) {
                    $room = new \app\admin\model\jdy\Room();
                }
                $room->building_id = $buildingid;
                $room->roomid = $row['id'];
                $room->name = $row['name'];
                $room->account = $row['account'];
                $room->password = $row['password'];
                $room->floorNo = $row['floorNo'];
                $room->optId = $row['optId'];
                $room->padId = $row['padId'];
                $room->remark = $row['remark'];
//                    $room->updatenum = $room->updatenum + 1;
                $room->save();

                //绑定案场
                $anchang = \app\admin\model\jdy\Anchang::where('remark', $row['name'])->find();
                if ($anchang) {
                    $anchang->building_id = $buildingid;
                    $anchang->room_id = $room->id;
                    $anchang->save();
                }

                $room = $room->toArray();
                $rooms[] = $room;
            }
        }

        $num = 0;
        foreach ($rooms as $room) {
            //获取装置
            $inputA = array(
                "action" => "Devices",
                "token" => $token,
                "remark" => $room['padId'],
            );
            $input = json_encode($inputA);
            curl_setopt($ch, CURLOPT_URL, $url . $input);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);// 执行并获取返回结果
            $data = curl_exec($ch);
            $ret = json_decode($data, TRUE);
            if ($ret['result'] && $ret['msg_code'] == 0) {
                $message = $ret['message'];
//                echo '<br>' . $message;
                $data = json_decode($message, TRUE);
                $total = $data["total"];
                $devicelist = $data["rows"];
                $size = count($devicelist);

                foreach ($devicelist as $row) {
                    $device = \app\admin\model\jdy\Device::where('deviceid', $row['id'])->find();
                    if (!$device) {
                        $device = new \app\admin\model\jdy\Device();
                    }
                    $device->room_id = $room['id'];
                    $device->deviceid = $row['id'];
                    $device->category = $row['category'];
                    $device->type = $row['type'];
                    $device->pid = $row['pid'];
                    $device->name = $row['name'];
                    $device->place = $row['place'];
                    $device->version = $row['version'];
                    $device->online = $row['online'];
                    $device->iconVer = $row['iconVer'];
//                    $device->iconUrl = $row['iconUrl'];
                    $device->center = $row['center'];
                    $device->deviceIp = $row['deviceIp'];
                    $device->routeMacString = $row['routeMacString'];
                    $device->subType = $row['subType'];
                    $device->state = $row['state'];
//                    $device->updatenum = $device->updatenum + 1;
                    $device->save();

                    $num++;
                }
            }
        }
        curl_close($ch);

        //添加装置的警告设置
        $jdyalarm = new Jdyalarm();
        $jdyalarm->addsetfordevice();

        //添加案场设备
        $this->drsb();

        echo $num.'<br>';
        echo $data0.' ~ ' . date('H:i:s') . '<br>';
    }

    public function drsb()
    {
        $sclist = \app\admin\model\jdy\Smallcategory::where('bigcategory_id', 4)->select();

        $acmodel = new \app\admin\model\jdy\Anchang();
        $list = $acmodel->field("id")->select();
        foreach ($list as $anchang) {
            $acdevice = new \app\admin\model\jdy\Acdevice();

            $list0 = $acdevice->where('anchang_id', $anchang->id)->select();

            $dataset = [];
            foreach ($sclist as $row) {
                $find = 0;
                if ($list0) {
                    foreach ($list0 as $row0) {
                        if ($row0->stype_id == $row->id) {
                            $find = 1;
                            break;
                        }
                    }
                }
                if ($find == 0) {
                    $dataset[] = ['anchang_id' => $anchang->id, 'stype_id' => $row->id, 'name' => $row->name];
                }
            }

            $acdevice->saveAll($dataset);
        }
    }

    //http://acciot.test/api/jdy/getzipcodetw?address=基隆市七堵區俊賢路59號5樓
    //http://zipcode.mosky.tw/api/find?address=基隆市七堵區俊賢路59號5樓
    public function getzipcodetw($address)
    {
        if (!$address || empty($address)) {
            echo '<br>'.'address:' . $address. '<br>';
            return "";
        }

        $url = "http://zipcode.mosky.tw/api/find?address=" . $address;
        $data = Http::get($url);
        $ret = json_decode($data, TRUE);
        if ($ret['result']) {
            echo '<br>'.$ret['result']. '<br>';
            return $ret['result'];
        }
        return "";
    }

    //数据迁移
    //http://acciot.test/api/jdy/aciotresave
    public function aciotresave()
    {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        ob_end_clean();
        ob_implicit_flush();
        header('X-Accel-Buffering: no');

        $data0 = date('H:i:s');

        $sql = "select * from buildings WHERE 1";
        $buildinglist = Db::query($sql);

//<----只运行一次
//计数zipcode
        foreach ($buildinglist as $building) {
            $remark = $building['remark'];
            if (empty($remark)) {
                continue;
            }
            $r = explode('-', $remark);
            $zipcode = $r[0];
            $zcode = \app\admin\model\jdy\Zipcodenum::where('zipcode', $zipcode)->find();
            if (!$zcode) {
                $zcode = new \app\admin\model\jdy\Zipcodenum();
                $zcode->zipcode = $zipcode;
                $zcode->num = 1;
            } else {
                $zcode->num = $zcode->num + 1;
            }
            $zcode->save();
        }
//--->
        //原设备
        $sql = "select cat_sn, comm_id, name, warranty_date, updated_by from user_dev WHERE 1";
        $userdevlist = Db::query($sql);

        //缴费记录
        $sql = "select * from fee_log WHERE 1";
        $feeloglist = Db::query($sql);

        //维修记录
//        $sql = "select * from repair WHERE 1";
//        $repairlist = Db::query($sql);
//
//        //task记录
//        $sql = "select * from task WHERE 1";
//        $tasklist = Db::query($sql);

        //----------
        $sql = "select * from sys_user WHERE 1";
        $sysuserlist = Db::query($sql);

        $roomlist = \app\admin\model\jdy\Room::with(['building'])->select();
        if ($roomlist) {
            $roomlist = collect($roomlist)->toArray();
        }

        $sql = "select code, name from city WHERE 1";
        $citylist = Db::query($sql);

        $sql = "select id, city_code, name from town WHERE 1";
        $townlist = Db::query($sql);

        $num = 0;
        foreach ($buildinglist as $building) {
            $room = null;
            foreach ($roomlist as $one) {
                if ($one['building']['name'] == $building['name']) {
                    $room = $one;
                    break;
                }
            }

            if (!$room) {
                continue;
            }

            $anchang = \app\admin\model\jdy\Anchang::where('name', $building['name'])->find();
            if (!$anchang) {
                $anchang = new \app\admin\model\jdy\Anchang();
            }

            $anchang->building_id = $room['building_id'];
            $anchang->room_id = $room['id'];
            $anchang->name = $room['building']['name'];
            $anchang->addr = $room['building']['address'];
            $anchang->remark = $room['building']['remark'];

            $anchang->contact1_id = 0;
            $anchang->contact2_id = 0;
            if (!empty($building['comm_contact_phone'])) {
                $contact1 = \app\admin\model\jdy\Contact::where('mobile', $building['comm_contact_phone'])->find();
                if (!$contact1) {
                    $contact1 = new \app\admin\model\jdy\Contact();
                    $contact1->name = $building['comm_contact_person'];
                    $contact1->mobile = $building['comm_contact_phone'];
                    $contact1->save();
                }
                $anchang->contact1_id = $contact1->id;
            }

            if (!empty($building['comm_contact_phone2'])) {
                $contact2 = \app\admin\model\jdy\Contact::where('mobile', $building['comm_contact_phone2'])->find();
                if (!$contact2) {
                    $contact2 = new \app\admin\model\jdy\Contact();
                    $contact2->name = $building['comm_contact_person2'];
                    $contact2->mobile = $building['comm_contact_phone2'];
                    $contact2->save();
                }
                $anchang->contact2_id = $contact2->id;
            }

            $anchang->companywindow_id = 0;
            if (intval($building['contact_user_sn']) > 0) {
                foreach ($sysuserlist as $sysuser) {
                    if ($building['contact_user_sn'] == $sysuser['sn']) {
                        $admin = \app\admin\model\Admin::where('username', $sysuser['account'])->find();
                        if (!$admin) {
                            $admin = new \app\admin\model\Admin();
                            $admin->username = $sysuser['account'];
                            $admin->nickname = $sysuser['name'];
                            $admin->password = '075eaec83636846f51c152f29b98a2fd';//初始密码123456
                            $admin->salt = 's4f3';
                            if ($sysuser['email'] != null) {
                                $admin->email = $sysuser['email'];
                            } else {
                                $admin->email = $sysuser['account'] . "@qq.com";
                            }
                            if ($sysuser['phone']) {
                                $admin->phone = $sysuser['phone'];
                            }
                            $admin->save();
                        }
                        $anchang->companywindow_id = $admin->id;

                        $aga = \app\admin\model\AuthGroupAccess::where('uid', $admin->id)->find();
                        if (!$aga) {
                            $aga = new \app\admin\model\AuthGroupAccess();
                            $aga->uid = $admin->id;
                            $aga->group_id = 3;
                            if ($sysuser['role'] == 'Admin') {
                                $aga->group_id = 2;
                            } else if ($sysuser['role'] == 'General') {
                                $aga->group_id = 4;
                            } else if ($sysuser['role'] == 'Customer') {
                                $aga->group_id = 5;
                            } else if ($sysuser['role'] == 'Office') {
                                $aga->group_id = 6;
                            }
                            $aga->save();
                        }
                        break;
                    }
                }
            }

            $anchang->xianshi = "";
            $anchang->xzq = "";
            foreach ($citylist as $city) {
                if ($city['code'] == $building['city_code']) {
                    $anchang->xianshi = $city['name'];
                    break;
                }
            }
            foreach ($townlist as $town) {
                if ($town['id'] == $building['town_code']) {
                    $anchang->xzq = $town['name'];
                    break;
                }
            }

            $anchang->mapurl = $building['gmap_link'];
            if ($building['fee_start_date'] != null) {
                $anchang->orderdate = $building['fee_start_date'];
            }

            $anchang->fee = $building['fee'];
            $anchang->fee_type = $building['fee_type'];
            $anchang->pay_date = $building['pay_date'];
            $anchang->is_pay = $building['is_pay'];
            $anchang->floor_no = $building['floor_no'];

            if (empty($anchang->remark)) {
                $zipcode = $this->getzipcodetw($anchang->addr);
                if (!empty($zipcode)) {
                    $zcode = \app\admin\model\jdy\Zipcodenum::where('zipcode', $zipcode)->find();
                    if (!$zcode) {
                        $zcode = new \app\admin\model\jdy\Zipcodenum();
                        $zcode->zipcode = $zipcode;
                        $zcode->num = 1;
                    } else {
                        $zcode->num = $zcode->num + 1;
                    }
                    $zcode->save();
                    $anchang->remark = $zipcode . "-" . $zcode->num;
                }
            }

            $anchang->save();
            $anchang_id = $anchang->id;

            foreach ($userdevlist as $userdev) {
                if ($userdev['comm_id'] == $building['id']) {
                    $acdevice = new  \app\admin\model\jdy\Acdevice();
                    $acdevice->anchang_id = $anchang_id;
                    $acdevice->stype_id = $userdev['cat_sn'];
                    $acdevice->name = $userdev['name'];
                    $acdevice->bgdate = $userdev['warranty_date'];
                    $acdevice->save();
                }
            }

            foreach ($feeloglist as $feelog) {
                if ($feelog['comm_id'] == $building['id']) {
                    $fee = new  \app\admin\model\jdy\Feelog();
                    $fee->anchang_id = $anchang_id;
                    $fee->pay_date = $feelog['pay_date'];
                    $fee->fee = $feelog['fee'];
                    $fee->fee_type = $feelog['fee_type'];
                    $fee->is_pay = $feelog['is_pay'];
                    $fee->opman = $feelog['created_by'];
                    $fee->save();
                }
            }

//            foreach ($repairlist as $repairone) {
//                if ($repairone['comm_id'] == $building['id']) {
//                    $repair = new  \app\admin\model\jdy\Repairlog();
//                    $repair->anchang_id = $anchang_id;
//                    $repair->msg = $repairone['content'];
//                    $repair->opman = $repairone['update_user_name'];
//                    $repair->save();
//                }
//            }

//            foreach ($tasklist as $taskone) {
//                if ($taskone['comm_id'] == $building['id']) {
//                    $task = new  \app\admin\model\jdy\Task();
//                    $task->anchang_id = $anchang_id;
//                    $task->pay_date = $taskone['pay_date'];
//                    $task->fee = $taskone['fee'];
//                    $task->fee_type = $taskone['fee_type'];
//                    $task->is_pay = $taskone['is_pay'];
//                    $task->opman = $taskone['created_by'];
//                    $task->save();
//                }
//            }

            $num++;
            echo $num.',';
        }

        echo '<br>'.$data0.' ~ ' . date('H:i:s') . '<br>';

    }

    //类别迁移
    //http://acciot.test/api/jdy/categoryresave
    public function categoryresave() {
        //大类别
        $sql = "select * from dev_big_cat WHERE 1";
        $bigcatlist = Db::query($sql);
        $blist = [];
        foreach ($bigcatlist as $bigcat) {
            $blist[] = array(
                "id" => $bigcat['sn'],
                "type" => $bigcat['id'],
                "name" => $bigcat['name'],
                "status" => $bigcat['status'],
                "opman" => $bigcat['updated_by']
            );
        }
        $bigcategory = new  \app\admin\model\jdy\Bigcategory();
        $bigcategory->insertAll($blist, true);

        //小类别
        $sql = "select * from dev_category WHERE 1";
        $scatlist = Db::query($sql);
        $slist = [];
        foreach ($scatlist as $scat) {
            $slist[] = array(
                "id" => $scat['sn'],
                "bigcategory_id" => $scat['parent_sn'],
                "type" => $bigcat['id'],
                "name" => $scat['name'],
                "alias" => $scat['alias'],
                "ignore_duration_time" => $scat['ignore_duration_time'],
                "status" => $scat['status'],
                "opman" => $scat['updated_by']
            );
        }
        $smallcategory = new  \app\admin\model\jdy\Smallcategory();
        $smallcategory->insertAll($slist, true);

        echo '<br>'. date('H:i:s') . '<br>';
    }

    //装置名称迁移
    //http://acciot.test/api/jdy/devicenameresave
    public function devicenameresave()
    {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        $sql = "select id, define_name from devices WHERE 1";
        $deviceslist = Db::query($sql);

        foreach ($deviceslist as $w) {
            $device = \app\admin\model\jdy\Device::where('deviceid', $w['id'])->find();
            if ($device) {
                $device->selfname = $w['define_name'];
                $device->save();
            }
        }
        echo '<br>' . date('H:i:s') . '<br>';
/*
UPDATE devices A, fa_jdy_device B
SET B.selfname = A.define_name
WHERE A.id=B.deviceid;

UPDATE devices A, fa_jdy_device B
SET B.smallcategory_id = A.s_cat_sn
WHERE A.id=B.deviceid;

*/
    }




    //发短信
    //http://acciot.test/api/jdy/sendmsg
    public function sendmsg() {

        $url = "http://jk.106api.cn/smsUTF8.aspx";
        $params = array(
            "type" => "send",
            "username" => "465979204",
            "password" => "A7D2D07BEEA0BB4025E6C9A804D31707",
            "gwid" => "daef24b",
            "mobile" => "13564508883",
            "message" => "【上海社保通】尊敬的**客户，感谢您选择上海社保通代缴业务，【公 积 金】业务代缴已经完成请于次月**日查询。",
        );
        $data = Http::post($url, $params);
        echo $data;
//        $ret = json_decode($data, TRUE);
//        if ($ret['result']) {
//            echo $ret['result'];
//        }

    }

    //http://acciot.test/api/jdy/test
    public function test()
    {

        $alog = \app\admin\model\jdy\Anchanglog::get(2);

        $prearray = json_decode($alog['pre'], TRUE);
        $nextarray = json_decode($alog['next'], TRUE);

        $change = "";
        foreach ($nextarray as $k => $v) {
            $v0 = $prearray[$k];
            if ($v0 != $v) {
                $change .= "[" . $k . "]" . $v0 . "=>" . $v . "; ";
            }
        }
        echo $change;

    }

    //http://acciot.test/api/jdy/receive?param=123&qq=332
//http://mg.acciot.com.tw/api/jdy/receive
    public function receive() {
//        $param = $this->request->request("param");
//        echo $param;

        $name = "";
        $change = "";
        $devicelog_id = 0;
        if ($_POST) {
            echo json_encode($_POST);
            $name = 'POST-- '.json_encode($_POST);

            $url = "http://ecapi.megotw.com/api/device/receive";
            $data = Http::post($url, $_POST);

            $device = \app\admin\model\jdy\Device::where('deviceid', $_POST['sn'])->find();
            if ($device) {
                $online = 1;
                if ($_POST['online'] == 'false') {
                    $online = 0;
                }
                if ($device->online != $online) {
                    $device->online_starttime = time();//秒 --- 持续时间

                    $devicelog = new \app\admin\model\jdy\Devicelog();
                    $devicelog->device_id = $device->id;
                    $devicelog->online = $online;
                    $devicelog->starttime = $device->online_starttime;
                    $devicelog->save();
                    $devicelog_id = $devicelog->id;
                }
                $device->online = $online;
                $device->type = $_POST['type'];
                $device->state = $_POST['status'];
                $device->update_time = date("Y-m-d H:i:s", time());
                $device->save();
                $change = "[已修改]";

                //即时播报
                $jdyalarm = new Jdyalarm();
                $jdyalarm->alarmjs($device->id);
            }

        } else if ($_GET) {
            echo json_encode($_GET);
            $name = 'GET-- '.json_encode($_GET);
        }
        $log = new \app\admin\model\jdy\Log();
        $log->name = $devicelog_id."~".$change.$name;
        $log->save();
        
        $this->success('接收成功');
    }

    //http://acciot.test/api/jdy/testMM
    public function testMM() {

        $isoline = 0;
        $dlog = \app\admin\model\jdy\Devicelog::order('id', 'desc')->limit(1)->find();
        if ($dlog) {
            $now = time();
            $h = strtotime($dlog['create_time']);
            $xx = $now - $h;
            if ($xx < 180) {
                $isoline = $xx;
            }
        }

    }

    //每日06:00 检查
    //http://acciot.test/api/jdy/checkdevivelog
    public function checkdevivelog()
    {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        $from = date("Y-m-d H:i:s", strtotime("-2 days", time()));

        $list = \app\admin\model\jdy\Devicelog::where('create_time', '>', $from)->order('id', 'desc')->select();
        $lasttime = 0;
        $num = 0;
        foreach ($list as $row) {
            $thistime = strtotime($row['create_time']);
            $xx = $lasttime - $thistime;
            if ($lasttime > 0 && $xx > 180) {
                //3分钟没有记录
                $der = \app\admin\model\jdy\Devicelogerr::where('from_devicelogid', $row->id)->find();
                if (!$der) {
                    $der = new \app\admin\model\jdy\Devicelogerr();
                    $der->from_devicelogid = $row->id;
                    $der->from_create_time = $row->create_time;
                    $der->to_create_time = date("Y-m-d H:i:s", $lasttime);
                    $der->save();
                    $num++;
                }
            }
            $lasttime = $thistime;
        }

        echo $num;
    }

    //重写案场联络人
    //http://acciot.test/api/jdy/rewriteangchangcontact
    public function rewriteangchangcontact()
    {
        $list = \app\admin\model\jdy\Anchang::all();

        foreach ($list as &$row) {

            if (intval($row['contact1_id']) > 0) {
                $contact1 = \app\admin\model\jdy\Contact::get($row['contact1_id']);
                if ($contact1) {
                    $row['contact1_name'] = $contact1->name;
                    $row['contact1_mobile'] = $contact1->mobile;
                }
            }
            if (intval($row['contact2_id']) > 0) {
                $contact2 = \app\admin\model\jdy\Contact::get($row['contact2_id']);
                if ($contact2) {
                    $row['contact2_name'] = $contact2->name;
                    $row['contact2_mobile'] = $contact2->mobile;
                }
            }
            $row->save();
        }
    }

    //同步maintenance的图片组到task
    //http://acciot.test/api/jdy/tbtaskmaintenance
    public function tbtaskmaintenance()
    {
        $list = \app\admin\model\jdy\Task::all();

        foreach ($list as &$row) {
            $imageslist = explode(',', $row['images']);
            $list1 = \app\admin\model\jdy\Maintenance::where("task_id", $row->id)->select();
            foreach ($list1 as $row1) {
                $imageslist1 = explode(',', $row1['images']);
                $imageslist = array_merge($imageslist, $imageslist1);
                $imageslist = array_unique($imageslist);
            }
            $row['images'] = implode(',', $imageslist);
            $row->save();
        }

        echo "ok!";
    }

    //http://acciot.test/api/jdy/testurl
    public function testurl()
    {
        $sql = "select id, openid from ims_ewei_shop_order where uniacid = 2 limit 3";
//        $list = Db::connect('mysql://megoshop:Aitrip123@106.14.209.254:3306/megoshop#utf8')
        $list = Db::connect('mysql://megoshop:Aitrip123@mg.acciot.com.tw:3306/megoshop#utf8')
            ->query($sql);
        $this->success('返回成功', $list);
    }

//调用类对象的函数
//$jdy = new Jdy();
//$a = $jdy->qqq();
    public function qqq()
    {
        return 2;
    }


}



//$url = "http://52php.cnblogs.com";
//$jsonStr = json_encode(array('a' => 1, 'b' => 2, 'c' => 2));
//list($returnCode, $returnContent) = http_post_json($url, $jsonStr);

// https://www.jianshu.com/p/79cbfeb19863
//命令行curl模拟get post请求
//Get
// curl http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?requestToken&username=enaviapi&password=enavi28960818
//Post
//curl -d "userName=enaviapi&password=enavi28960818" http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?requestToken


//http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?queryDatas&requestInfoJson={"action":"Buildings","token":"A3B92EFE","page":1,"rows":20,"remark":""}
//http://s1.yourslink.com:8080/shs-manager/httpApiTWController.do?requestToken&username=enaviapi&password=enavi28960818

//rooms
/*
[
{"id":"402881e55fc8ced9015fc8cfc9b50000","name":"314","floorNo":"2","optId":null,"account":null,"password":null,"padId":"wifino8","createTime":null,"remark":"",
"building":{"id":"402881ed5b51b3e6015b51b4b32b0001","floorNumber":30,"address":"云海物联","name":"主楼","optId":null,"createTime":null,"remark":"","coding":null}},

{"id":"402881ed5b51b99e015b51bc01090004","name":"315","floorNo":"3","optId":null,"account":null,"password":null,"padId":"wifino6","createTime":null,"remark":"111",
"building":{"id":"402881ed5b51b3e6015b51b4b32b0001","floorNumber":30,"address":"云海物联","name":"主楼","optId":null,"createTime":null,"remark":"","coding":null
]
*/
//device
/*
[
{"category":0,"center":true,"deviceIp":"192.168.1.102","iconVer":0,"id":"0040D63C100205","name":"23741-1B1F11車位","online":true,"pid":"0040D63C100205","place":"","routeMacString":"1831BF594B7C","state":"","subType":0,"type":9,"version":0},
{"category":0,"center":false,"deviceIp":"192.168.1.102","iconUrl":"","iconVer":0,"id":"10525301010466","name":"清BW","online":true,"pid":"0040D63C100205","place":"","routeMacString":"1831BF594B7C","state":"E020","subType":0,"type":16,"version":0}
]
*/